<?php
namespace app\backend\models;

use app\backend\lib\ArDBLib;
use lib\Tool\Tools;

class BackendMenuModel extends ArDBLib{

    public static function tableName(){
        return '@backend_menu';
    }

    /**
     * 获取列表全部数据
     * @param array $where
     * @return array
     */
    public function getMenuAll(){
        //判断当前层级
        $controller = Tools::My()->request()->get('c','');
        $max_menu_depth = self::find()->select('MAX(menu_depth) AS menu_depth')->where([['=','is_del',0]])->one()?:[];
        $max_menu_depth = $max_menu_depth[0]['menu_depth'];
        $data = self::find()->where([['=','is_del',0]])->all();
        array_multisort(array_column($data,'menu_depth'), SORT_DESC, $data);
        $data = array_column($data,null,'menu_id');
        for ($i=0;$i < $max_menu_depth; $i++){
            foreach ($data as $k=>&$v){
                if (!empty($v['parent_id'])){
                    if (empty($v['menu_m']) || empty($v['menu_c'])){
                        $v['menu_url'] = '#';
                    }else{
                        $v['menu_url'] = Tools::My()->url($v['menu_c'].'/'.$v['menu_m']);
                    }
                    $data[$v['parent_id']]['children'][] = $v;
                    $data[$v['parent_id']]['current_level'] = $controller == $v['menu_c'] ?true:false;
                    unset($data[$k]);
                }

            }
        }
        return $data;
    }

}